var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
var Bmob = require('../../utils/bmob.js')
Page({
  data: {
    tabs: ["联系我们", "公司简介"],
    activeIndex: 0,
    sliderOffset: 0,
    contab: "联系方式",
    sliderLeft: 0,
    markers: [{
      iconPath: "../../images/map.png",
      latitude: 34.3296900000,
      longitude: 108.9848800000,
      title: '123'
    }]
  },
  onLoad: function () {
    var me = this;
    wx.getSystemInfo({
      success: function (res) {
        me.setData({
          sliderLeft: (res.windowWidth / me.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / me.data.tabs.length * me.data.activeIndex
        });
      }
    });

    let sysParam = wx.getStorageSync("sysParam");
    if (!sysParam) {
      me.loadSysParam();
    } else {
      me.setSysData(sysParam);
    }
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  //打开地图
  openLocation: function () {
    var me = this;
    wx.openLocation({
      address: me.data.detailAddress,
      name: me.data.title,
      latitude: parseFloat(me.data.latitude),
      longitude: parseFloat(me.data.longitude),
      success: function () {
      },
      fail: function () {
      },
      complete: function () {
      }
    });
  },
  //拨打电话
  makePhoneCall: function (e) {
    var me = this;
    console.log(e);
    wx.makePhoneCall({
      phoneNumber: e.target.dataset.number
    });
  },
  //加载系统参数
  loadSysParam: function () {
    var me = this;
    var SysParam = Bmob.Object.extend("SysParam");
    var query = new Bmob.Query(SysParam);
    query.containedIn("code", ['SYS_INTRO', "SYS_ADDRESS", "SYS_MOBILE", "SYS_MOBILE2", "SYS_EMAIL", "SYS_SITE",
    "SYS_FAX", "SYS_ADDRESS_LATITUDE", "SYS_ADDRESS_LONGITUDE", "SYS_TITLE"]);
    query.find({
      success: function (results) {
        var sysParam = wx.getStorageSync("sysParam") || {};
        for (var i = 0; i < results.length; i++) {
          var code = results[i].get("code");
          var value = results[i].get("value");
          sysParam[code] = value;
        }
        me.setSysData(sysParam);
        wx.setStorageSync("sysParam", sysParam);
      },
      error: function (error) {
        console.log(error, "error");
      }
    });
  },
  /**设置系统参数值 */
  setSysData: function (sysParam) {
    var me = this;
    me.setData({
      intro: sysParam.SYS_INTRO,
      address: " " + sysParam.SYS_ADDRESS,
      detailAddress: sysParam.SYS_ADDRESS,
      mobile: " " + sysParam.SYS_MOBILE,
      mobile2: " " + sysParam.SYS_MOBILE2,
      latitude: parseFloat(sysParam.SYS_ADDRESS_LATITUDE),
      longitude: parseFloat(sysParam.SYS_ADDRESS_LONGITUDE),
      title: sysParam.SYS_TITLE,
      email: " " + sysParam.SYS_EMAIL,
      site: " " + sysParam.SYS_SITE,
      markers: [{
        iconPath: "/images/map.png",
        latitude: parseFloat(sysParam.SYS_ADDRESS_LATITUDE),
        longitude: parseFloat(sysParam.SYS_ADDRESS_LONGITUDE),
        //title: sysParam.SYS_TITLE,
        callout: {
          content: sysParam.SYS_TITLE,
          padding: 20,
          fontSize: 16,
          color: '#1296db',
          bgColor: '#F5FFFA',
          borderRadius: 8,
          display: 'ALWAYS'
        }
      }]
    });
  },
  //设置转发分享
  onShareAppMessage: function () {
    title: app.sysParam.sysTitle
  }
});